package com.cloud.logger.zchain.zchainserver.controller;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.core.exception.BusinessException;
import com.cloud.logger.zchain.zchainserver.entity.AngelaLoggerProject;
import com.cloud.logger.zchain.zchainserver.entity.AngelaLoggerProjectNode;
import com.cloud.logger.zchain.zchainserver.entity.ProjectUserRelation;
import com.cloud.logger.zchain.zchainserver.mongo.AngelaProjectNodeRepository;
import com.cloud.logger.zchain.zchainserver.mongo.AngelaProjectRepository;
import com.cloud.logger.zchain.zchainserver.mongo.ProjectUserRelationRepository;
import com.cloud.logger.zchain.zchainserver.service.AngelaLoggerProjectService;
import com.cloud.logger.zchain.zchainserver.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/11/7
 * Time: 13:31
 */
@RestController
@RequestMapping(value = "logger_project")
@Api(value = "/日志相关景区项目查询模块", description = "项目管理")
public class AngelaLoggerProjectCtl {

    @Resource
    private AngelaProjectRepository angelaProjectRepository;

    @Resource
    private ProjectUserRelationRepository projectUserRelationRepository;

    @Resource
    private ProjectService projectService;

    @Resource
    private AngelaProjectNodeRepository nodeRepository;

    @Resource
    AngelaLoggerProjectService angelaLoggerProjectService;

    @PostMapping(value = "")
    @ApiOperation(value = "保存日志项目信息")
    public ResultMessage saveLoggerProject(@RequestBody @Validated AngelaLoggerProject project) throws BusinessException {
        return projectService.save(project);
    }

    @PutMapping(value = "")
    @ApiOperation(value = "更新日志项目信息")
    public ResultMessage updateLoggerProject(@RequestBody @Validated AngelaLoggerProject project) throws BusinessException {
        return projectService.updateProject(project);
    }

    @GetMapping(value = "{id}")
    @ApiOperation(value = "通过id获取信息")
    public ResultMessage<AngelaLoggerProject> getLoggerProject(@PathVariable String id){
        ResultMessage resultMessage = new ResultMessage();
        AngelaLoggerProject resultinfo = angelaProjectRepository.findOne(id);

        if (resultinfo !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setContent(resultinfo);
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") String id) {
        return projectService.removeByParentId(id);
    }

    @ApiOperation(value = "获取树形结构的项目结构")
    @GetMapping(value = "findLoggerProjectTree")
    public ResultMessage findLoggerProject(){
        return angelaLoggerProjectService.findLoggerProject();
    }

    @PostMapping(value = "saveProjectUserRelation")
    @ApiOperation(value = "保存项目用户关系信息")
    public ResultMessage saveUser(@RequestBody ProjectUserRelation projectUserRelation){
        ResultMessage resultMessage = new ResultMessage();

        projectUserRelation = projectUserRelationRepository.save(projectUserRelation);

        if (projectUserRelation.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(projectUserRelation.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @ApiOperation(value = "通过id删除项目用户关系")
    @DeleteMapping(value = "relation/{id}")
    public ResultMessage removeOrg(@PathVariable("id") String id) {
        ResultMessage resultMessage = new ResultMessage();
        projectUserRelationRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

    @ApiOperation(value = "通过项目id获取关联的所有项目下的用户")
    @GetMapping(value = "relation/{id}")
    public ResultMessage getUsersByProjectId(@PathVariable("id") String id) {
        ResultMessage resultMessage = new ResultMessage();

        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setContent(projectService.getUsersByProjectId(id));
        return  resultMessage;
    }

    @ApiOperation(value = "通过项目code获取分布式项目下的子项目信息")
    @GetMapping(value = "distributed/{code}")
    public ResultMessage<List<AngelaLoggerProjectNode>> getProjectNodeByProjectId(@PathVariable("code") String code) {
        ResultMessage resultMessage = new ResultMessage();

        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setContent(nodeRepository.findByCode(code));
        return  resultMessage;
    }

    @ApiOperation(value = "通过子项目id删除分布式项目数据")
    @DeleteMapping(value = "distributed/{id}")
    public ResultMessage<List<AngelaLoggerProjectNode>> deletedProjectNodeById(@PathVariable("id") String id) {
        ResultMessage resultMessage = new ResultMessage();
        nodeRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

    @ApiOperation(value = "更新分布式项目信息")
    @PutMapping(value = "distributed")
    @ResponseBody
    public ResultMessage updateProjectNode(@RequestBody AngelaLoggerProjectNode angelaLoggerProjectNode) {
        return angelaLoggerProjectService.updateProjectNode(angelaLoggerProjectNode);
    }


    @PostMapping(value = "saveProjectMultiUserRelation")
    @ApiOperation(value = "保存项目和多用户关系信息 by-zhengyt")
    public ResultMessage saveProjectMultiUserRelation(@RequestBody ProjectUserRelation projectUserRelation){
        return angelaLoggerProjectService.saveProjectMultiUserRelation(projectUserRelation);
    }

}
